package com.example.demo2.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo2.common.Result;
import com.example.demo2.common.TokenUtil;
import com.example.demo2.entity.DTO.SysAdminDTO;
import com.example.demo2.entity.SysAdmin;
import com.example.demo2.exception.ServiceException;
import com.example.demo2.mapper.SysAdminMapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysAdminService extends ServiceImpl<SysAdminMapper, SysAdmin> {
    public Result getOneOfsysAdmin(SysAdminDTO sysAdminDTO) {
        if (Strings.isBlank(sysAdminDTO.getUsername()) || Strings.isEmpty(sysAdminDTO.getUsername()) || Strings.isBlank(sysAdminDTO.getPassword()) || Strings.isEmpty(sysAdminDTO.getPassword())) {
            throw new ServiceException("-1", "参数错误");
        }
        LambdaQueryWrapper<SysAdmin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysAdmin::getUsername, sysAdminDTO.getUsername());
        lqw.eq(SysAdmin::getPassword, sysAdminDTO.getPassword());
        SysAdmin one = getOne(lqw);
        if (one != null) {
            String token = TokenUtil.getToken(one.getId().toString(), one.getPassword());
            sysAdminDTO.setToken(token);
            return Result.success(sysAdminDTO);
        } else {
            return Result.error("账号或密码错误", "-1");
        }
    }

    public Result getBaseInfo() {
        List<SysAdmin> list = list();
        return Result.success(list);
    }

    public Result updateInfo(SysAdmin sysAdmin) {
        boolean b = update(sysAdmin,null);
        if (b) {
            return Result.success(b);
        } else {
            return Result.error("修改失败", "-1");
        }
    }
}
